{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "用样本均值、方差估计a=0.9697, b=2.0336\n",
      "用样本矩估计a=0.9832, b=2.0201\n",
      "最大似然估计a=1.0340, b=1.9670\n"
     ]
    }
   ],
   "source": [
    "#例6-7，6-9，6-10\n",
    "import numpy as np\n",
    "from scipy.stats import uniform\n",
    "x=np.array([1.248, 1.664 ,1.101 ,1.967 ,1.468,\n",
    "            1.140, 1.434, 1.063, 1.878, 1.375,\n",
    "            1.819, 1.704, 1.328, 1.619, 1.830,\n",
    "            1.764, 1.034, 1.553, 1.878, 1.166])\n",
    "mu=x.mean()\n",
    "sigma=x.std(ddof=1)\n",
    "a=mu-np.sqrt(3)*sigma\n",
    "b=mu+np.sqrt(3)*sigma\n",
    "print('用样本均值、方差估计a=%.4f, b=%.4f'%(a, b))\n",
    "l, s=uniform.fit_loc_scale(x)\n",
    "a=l\n",
    "b=a+s\n",
    "print('用样本矩估计a=%.4f, b=%.4f'%(a, b))\n",
    "l, s=uniform.fit(x)\n",
    "a=l\n",
    "b=a+s\n",
    "print('最大似然估计a=%.4f, b=%.4f'%(a, b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "N=6, p=0.7470\n"
     ]
    }
   ],
   "source": [
    "#练习6-6\n",
    "import numpy as np\n",
    "from scipy.stats import binom\n",
    "x=np.array([7, 4, 6, 4, 5, 5, 2, 6, 5, 4,\n",
    "            5, 3, 5, 4, 4, 3, 5, 4, 4, 6,\n",
    "            6, 6, 5, 6, 4, 6, 4, 4, 3, 5,\n",
    "            4, 5, 4, 5, 6, 6, 4, 5, 5, 4,\n",
    "            3, 7, 4, 5, 3, 4, 5, 4, 4, 5])\n",
    "mu=x.mean()\n",
    "s2=x.var(ddof=1)\n",
    "b2=x.var()\n",
    "p=1-s2/mu\n",
    "N=mu**2/(mu-s2)\n",
    "print('N=%d, p=%.4f'%(N,p))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "mu=74.0020\n",
      "sigma^2=0.00001\n",
      "s^2=0.00001\n"
     ]
    }
   ],
   "source": [
    "#例6-8\n",
    "import numpy as np\n",
    "x=np.array([74.001, 74.005, 74.003, 74.001,\n",
    "            74.000, 73.998, 74.006, 74.002])\n",
    "a1=x.mean()\n",
    "print('mu=%.4f'%a1)\n",
    "a2=(x**2).mean()\n",
    "print('sigma^2=%.5f'%(a2-a1**2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "theta=0.83\n"
     ]
    }
   ],
   "source": [
    "#练习6-7\n",
    "import numpy as np\n",
    "x=np.array([1, 2, 1])\n",
    "mean=x.mean()\n",
    "theta=(3-mean)/2\n",
    "print('theta=%.2f'%theta)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "lambda的矩估计值=1064.4107\n",
      "lambda的最大似然估计值=1040.0000\n"
     ]
    }
   ],
   "source": [
    "#练习6-8，6-9\n",
    "import numpy as np\n",
    "from scipy.stats import expon\n",
    "x=np.array([1050, 1100, 1080, 1120, 1200, 1250, 1040, 1130, 1300, 1200])\n",
    "l, _=expon.fit_loc_scale(x)\n",
    "print('lambda的矩估计值=%.4f'%l)\n",
    "l, _=expon.fit(x)\n",
    "print('lambda的最大似然估计值=%.4f'%l)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(100.131, 102.091)\n"
     ]
    }
   ],
   "source": [
    "#例6-23\n",
    "import numpy as np\n",
    "from utility import muBounds\n",
    "x=np.array([99.3, 104.7, 100.5, 101.2, 99.7, 98.5, 102.8, 103.3, 100.0])\n",
    "mean=x.mean()\n",
    "sigma=1.5\n",
    "n=x.size\n",
    "s=sigma/np.sqrt(n)\n",
    "confidence=0.95\n",
    "a, b=muBounds(mean, s, confidence)\n",
    "print('(%.3f, %.3f)'%(a, b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(5.608, 6.392)\n"
     ]
    }
   ],
   "source": [
    "#练习6-22\n",
    "import numpy as np\n",
    "from utility import muBounds\n",
    "x=np.array([6.0, 5.7, 5.8, 6.5, 7.0, 6.3, 5.6, 6.1, 5.0])\n",
    "mean=x.mean()\n",
    "sigma=0.6\n",
    "n=x.size\n",
    "d=sigma/np.sqrt(n)\n",
    "confidence=0.95\n",
    "a, b=muBounds(mean, d, confidence)\n",
    "print('(%.3f, %.3f)'%(a, b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(14.782, 15.058)\n"
     ]
    }
   ],
   "source": [
    "#例6-24\n",
    "from utility import muBounds\n",
    "x=np.array([14.6, 15.0, 14.7, 15.1, 14.9, 14.8, 15.0, 15.1, 15.2, 14.8])\n",
    "mean=x.mean()\n",
    "s=x.std(ddof=1)\n",
    "n=x.size\n",
    "d=s/np.sqrt(n)\n",
    "confidence=0.95\n",
    "a, b=muBounds(mean, d, confidence, df=n-1)\n",
    "print('(%.3f, %.3f)'%(a, b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(5.558, 6.442)\n"
     ]
    }
   ],
   "source": [
    "#练习6-23\n",
    "import numpy as np\n",
    "from utility import muBounds\n",
    "x=np.array([6.0, 5.7, 5.8, 6.5, 7.0, 6.3, 5.6, 6.1, 5.0])\n",
    "mean=x.mean()\n",
    "s=x.std(ddof=1)\n",
    "n=x.size\n",
    "d=s/np.sqrt(n)\n",
    "confidence=0.95\n",
    "a, b=muBounds(mean, d, confidence, df=n-1)\n",
    "print('(%.3f, %.3f)'%(a, b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "mu>=1086.440\n"
     ]
    }
   ],
   "source": [
    "#例6-25\n",
    "from utility import muBound\n",
    "import numpy as np\n",
    "x=np.array([1050, 1100, 1120, 1250,  1280])\n",
    "mean=x.mean()\n",
    "sigma=100\n",
    "n=x.size\n",
    "confidence=0.95\n",
    "d=sigma/np.sqrt(n)\n",
    "a=muBound(mean, d, confidence)\n",
    "print('mu>=%.3f'%a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "mu<=6.329\n"
     ]
    }
   ],
   "source": [
    "#练习6-24\n",
    "import numpy as np\n",
    "from utility import muBound\n",
    "x=np.array([6.0, 5.7, 5.8, 6.5, 7.0, 6.3, 5.6, 6.1, 5.0])\n",
    "mean=x.mean()\n",
    "s=0.6\n",
    "n=x.size\n",
    "d=s/np.sqrt(n)\n",
    "confidence=0.95\n",
    "a=muBound(mean, d, confidence, low=False)\n",
    "print('mu<=%.3f'%a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "mu>=38334.786\n"
     ]
    }
   ],
   "source": [
    "#例6-26\n",
    "import numpy as np\n",
    "from utility import muBound\n",
    "mean=41116\n",
    "sigma=6346\n",
    "n=16\n",
    "s=sigma/np.sqrt(n)\n",
    "confidence=0.95\n",
    "a=muBound(mean, s, confidence, n-1)\n",
    "print('mu>=%.3f'%a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "mu<=39.325\n"
     ]
    }
   ],
   "source": [
    "#练习6-25\n",
    "import numpy as np\n",
    "from utility import muBound\n",
    "x=np.array([40, 36, 23, 40, 31, 33, 49, 33, 34, 43, 26, 39])\n",
    "mean=x.mean()\n",
    "s=x.std(ddof=1)\n",
    "n=x.size\n",
    "d=s/np.sqrt(n)\n",
    "confidence=0.95\n",
    "a=muBound(mean, d, confidence, n-1, low=False)\n",
    "print('mu<=%.3f'%a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(601.1796, 4234.9817)\n"
     ]
    }
   ],
   "source": [
    "#例6-27\n",
    "import numpy as np\n",
    "from utility import sigma2Bounds\n",
    "x=np.array([482, 493, 475, 471, 510,\n",
    "            446, 435, 418, 394, 469])\n",
    "s2=x.var(ddof=1)\n",
    "n=x.size\n",
    "confidence=0.95\n",
    "d=(n-1)*s2\n",
    "a, b=sigma2Bounds(d, n-1, confidence)\n",
    "print('(%.4f, %.4f)'%(a, b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "mu in (181.894, 190.106)\n",
      "sigma in (9.7420, 15.7974)\n"
     ]
    }
   ],
   "source": [
    "#练习6-26\n",
    "import numpy as np\n",
    "from utility import muBounds, sigma2Bounds\n",
    "n=25\n",
    "mean=186\n",
    "s=12\n",
    "confidence=0.9\n",
    "d=s/np.sqrt(n)\n",
    "a, b=muBounds(mean, d, confidence, df=n-1)\n",
    "print('mu in (%.3f, %.3f)'%(a,b))\n",
    "d=(n-1)*(s**2)\n",
    "a, b=sigma2Bounds(d, n-1, confidence)\n",
    "print('sigma in (%.4f, %.4f)'%(np.sqrt(a), np.sqrt(b)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "mu>=70.0281\n",
      "sigma^2<=35.4849\n"
     ]
    }
   ],
   "source": [
    "#例6-28\n",
    "from utility import muBound, sigma2Bound \n",
    "n=20\n",
    "mean=72\n",
    "s=4\n",
    "confidence=0.98\n",
    "d=s/np.sqrt(n)\n",
    "b=muBound(mean,d,confidence, df=n-1)\n",
    "print('mu>=%.4f'%b)\n",
    "d=(n-1)*(s**2)\n",
    "b=sigma2Bound(d, n-1, confidence, low=False)\n",
    "print('sigma^2<=%.4f'%b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "sigma>=7.90\n"
     ]
    }
   ],
   "source": [
    "#练习6-27\n",
    "import numpy as np\n",
    "from utility import sigma2Bound\n",
    "n=9\n",
    "s2=11**2\n",
    "confidence=0.95\n",
    "b=sigma2Bound(s2, n, confidence)\n",
    "b=np.sqrt(b)\n",
    "print('sigma>=%.2f'%b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(3.0727, 4.9273)\n"
     ]
    }
   ],
   "source": [
    "#例6-29\n",
    "import numpy as np\n",
    "from utility import muBounds\n",
    "xmean=500\n",
    "ymean=496\n",
    "mean=xmean-ymean\n",
    "s1=1.10**2\n",
    "s2=1.20**2\n",
    "n1=10\n",
    "n2=20\n",
    "sw=np.sqrt(((n1-1)*s1+(n2-1)*s2)/(n1+n2-2))\n",
    "d=sw*np.sqrt(1/n1+1/n2)\n",
    "confidence=0.95\n",
    "a, b=muBounds(mean, d, confidence, n1+n2-2)\n",
    "print('(%.4f, %.4f)'%(a, b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(-4.1527, 0.1127)\n"
     ]
    }
   ],
   "source": [
    "#练习6-28\n",
    "import numpy as np\n",
    "from utility import muBounds\n",
    "xmean=91.73\n",
    "ymean=93.75\n",
    "mean=xmean-ymean\n",
    "s1=3.89\n",
    "s2=4.02\n",
    "n1=8\n",
    "n2=8\n",
    "sw=np.sqrt(((n1-1)*s1+(n2-1)*s2)/(n1+n2-2))\n",
    "d=sw*np.sqrt(1/n1+1/n2)\n",
    "confidence=0.95\n",
    "a, b=muBounds(mean, d, confidence, n1+n2-2)\n",
    "print('(%.4f, %.4f)'%(a, b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "mu1-mu2>=0.0074\n"
     ]
    }
   ],
   "source": [
    "#例6-30\n",
    "import numpy as np\n",
    "from utility import muBound\n",
    "xmean=45479.431\n",
    "ymean=45479.398\n",
    "mean=xmean-ymean\n",
    "s1=0.0440**2\n",
    "s2=0.0308**2\n",
    "n1=10\n",
    "n2=15\n",
    "sw=np.sqrt(((n1-1)*s1+(n2-1)*s2)/(n1+n2-2))\n",
    "d=sw*np.sqrt(1/n1+1/n2)\n",
    "confidence=0.95\n",
    "a=muBound(mean, d, confidence, n1+n2-2)\n",
    "print('mu1-mu2>=%.4f'%a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(-4.1527, 0.1127)\n"
     ]
    }
   ],
   "source": [
    "#练习6-29\n",
    "import numpy as np\n",
    "from utility import muBounds\n",
    "xmean=91.73\n",
    "ymean=93.75\n",
    "mean=xmean-ymean\n",
    "s1=3.89\n",
    "s2=4.02\n",
    "n1=8\n",
    "n2=8\n",
    "sw=np.sqrt(((n1-1)*s1+(n2-1)*s2)/(n1+n2-2))\n",
    "d=sw*np.sqrt(1/n1+1/n2)\n",
    "confidence=0.95\n",
    "a, b=muBounds(mean, d, confidence, n1+n2-2)\n",
    "print('(%.4f, %.4f)'%(a, b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(0.4539, 2.7911)\n"
     ]
    }
   ],
   "source": [
    "#例6-31\n",
    "import numpy as np\n",
    "from utility import sigma2RatioBounds\n",
    "s1=0.34\n",
    "s2=0.29\n",
    "n1=18\n",
    "n2=13\n",
    "d=s1/s2\n",
    "confidence=0.9\n",
    "a, b=sigma2RatioBounds(d, n1-1, n2-1, confidence)\n",
    "print('(%.4f, %.4f)'%(a, b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(0.2219, 3.5972)\n"
     ]
    }
   ],
   "source": [
    "#练习6-30\n",
    "import numpy as np\n",
    "from utility import sigma2RatioBounds\n",
    "s1=0.5419\n",
    "s2=0.6065\n",
    "n1=10\n",
    "n2=10\n",
    "d=s1/s2\n",
    "confidence=0.95\n",
    "a, b=sigma2RatioBounds(d, n1-1, n2-1, confidence)\n",
    "print('(%.4f, %.4f)'%(a, b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "sigma1^2/sigma2^2<=2.8403\n"
     ]
    }
   ],
   "source": [
    "#例6-32\n",
    "import numpy as np\n",
    "from utility import sigma2RatioBound\n",
    "s1=0.5419\n",
    "s2=0.6065\n",
    "n1=10\n",
    "n2=10\n",
    "d=s1/s2\n",
    "confidence=0.95\n",
    "a=sigma2RatioBound(d, n1-1, n2-1, confidence, low=False)\n",
    "print('sigma1^2/sigma2^2<=%.4f'%a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "sigma1^2/sigma2^2>=6.1744\n"
     ]
    }
   ],
   "source": [
    "#练习6-31\n",
    "from utility import sigma2RatioBound\n",
    "xmean=45479.431\n",
    "ymean=45479.398\n",
    "s1=0.0440**2\n",
    "s2=0.0308**2\n",
    "n1=10\n",
    "n2=15\n",
    "d=s1/s2\n",
    "confidence=0.95\n",
    "a=sigma2RatioBound(d, n1-1, n2-1, confidence, low=False)\n",
    "print('sigma1^2/sigma2^2>=%.4f'%a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
